﻿using Furion.DependencyInjection;
using GWB.Application.System.LogManage.Dtos;
using GWB.Core;
using GWB.Core.TableEntity;
using GWB.Supplier;
using Mapster;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace GWB.Application.System.LogManage.LogService
{
    /// <summary>
    /// 
    /// </summary>
    public class LogService : SqlSugarDb, ITransient, ILogService
    {
        /// <summary>
        /// 添加请求日志
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> AddRequestLog(RequestLogCreateOrUpdateInputDto inputDto)
        {
            return await SqlSugarDb.Db.Insertable(inputDto.Adapt<RequestLog>())
                .ExecuteCommandIdentityIntoEntityAsync();
            return true;
        }

        /// <summary>
        /// 查询请求日志
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PageResult<RequestLogOutDto>> RequestLogPage(RequestLogPageInputDto inputDto)
        {
            RefAsync<int> total = 0;
            var data =await SqlSugarDb.Db.Queryable<RequestLog>()
                .WhereIF(inputDto.ConsumedTime.HasValue, x => x.ConsumedTime >= inputDto.ConsumedTime)
                .WhereIF(!string.IsNullOrWhiteSpace(inputDto.CreateBy), x => x.CreateBy.Contains(inputDto.CreateBy))
                .WhereIF(inputDto.IsError.HasValue, x => x.IsError == inputDto.IsError)
                .OrderBy(x => x.CreateAt, OrderByType.Desc)
                .Select(x => new { x.Id, x.Ip, x.IsError, x.Method, x.OrgId, x.CreateAt, x.CreateBy, x.ConsumedTime, x.Annotation })
                .ToPageListAsync(inputDto.Current, inputDto.PageSize, total);
            return new PageResult<RequestLogOutDto>
            {
                Data = data.Adapt<List<RequestLogOutDto>>(),
                Total = total

            };
        }

        /// <summary>
        /// 查询请求日志详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<RequestLogOutDto> GetRequestLogInfo(long id)
        {
            var data = await SqlSugarDb.Db.Queryable<RequestLog>().FirstAsync(x => x.Id == id);
            return data.Adapt<RequestLogOutDto>();
        }



        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PageResult<ErrorLogOutDto>> ErrorLogPage(ErrorLogPageInputDto inputDto)
        {
            RefAsync<int> total = 0;
            var data = await SqlSugarDb.Db.Queryable<ErrorLog>()
                .WhereIF(inputDto.IsUnexpected.HasValue, x => x.IsUnexpected == inputDto.IsUnexpected.Value)
                .WhereIF(!string.IsNullOrWhiteSpace(inputDto.ErrorCode), x => x.ErrorCode.Contains(inputDto.ErrorCode))
                .OrderBy(x => x.CreateAt, OrderByType.Desc)
                .ToPageListAsync(inputDto.Current, inputDto.PageSize, total);
            return new PageResult<ErrorLogOutDto>
            {
                Data = data.Adapt<List<ErrorLogOutDto>>(),
                Total = total
            };
        }
    }
}
